using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace THSERP
{
    public partial class guiAddRemoveAttributes : Form
    {
        private int ICDepartmentID;
        public ProductAttributeList TreeList;
        private int NumLevels = 0;
        private String ErrorMessage;
        private List<int> listCheckEdit = new List<int>(); 
        public guiAddRemoveAttributes()
        {
            InitializeComponent();
        }

        public guiAddRemoveAttributes(ProductAttributeList treeList, int departmentID)
        {
            InitializeComponent();

            this.TreeList = treeList;
            this.ICDepartmentID = departmentID;
        }

        private void guiAddRemoveAttributes_Load(object sender, EventArgs e)
        {
            //Load department attributes
            ICDepartmentAttributesController objDepartmentAttributesController = new ICDepartmentAttributesController();
            ICDepartmentAttributeValuesController objDepartmentAttributeValuesController = new ICDepartmentAttributeValuesController();
            DataSet ds1 = objDepartmentAttributesController.GetAllAttributesByDepartmentID(ICDepartmentID);
            if (ds1.Tables.Count > 0)
            {
                //Store the number of levels for later use
                NumLevels = ds1.Tables[0].Rows.Count;

                int x = 10;
                foreach (DataRow row1 in ds1.Tables[0].Rows)
                {
                    int y = 10;
                    ICDepartmentAttributesInfo objDepartmentAttributesInfo = (ICDepartmentAttributesInfo)objDepartmentAttributesController.GetObjectFromDataRow(row1);
                    DevExpress.XtraEditors.LabelControl lbl = new DevExpress.XtraEditors.LabelControl();
                    lbl.Location = new Point(x, y);
                    lbl.Text = objDepartmentAttributesInfo.ICDepartmentAttributeName;
                    fld_pnlAttributes.Controls.Add(lbl);
                    DataSet ds2 = objDepartmentAttributeValuesController.GetAllDataByForeignColumn("FK_ICDepartmentAttributeID", objDepartmentAttributesInfo.ICDepartmentAttributeID);
                    foreach (DataRow row2 in ds2.Tables[0].Rows)
                    {
                        y += 30;
                        ICDepartmentAttributeValuesInfo objDepartmentAttributeValuesInfo = (ICDepartmentAttributeValuesInfo)objDepartmentAttributeValuesController.GetObjectFromDataRow(row2);
                        DevExpress.XtraEditors.CheckEdit chk = new DevExpress.XtraEditors.CheckEdit();
                        chk.Location = new Point(x, y);
                        chk.Text = objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueValue;
                        chk.Tag = objDepartmentAttributeValuesInfo;
                        chk.Properties.AccessibleName = objDepartmentAttributesInfo.ICDepartmentAttributeLevel.ToString();
                        if (TreeList.Exists("FK_ICDepartmentAttributeValueID", objDepartmentAttributeValuesInfo.ICDepartmentAttributeValueID))
                            chk.Checked = true;
                        else
                            chk.Checked = false;
                        this.fld_pnlAttributes.Controls.Add(chk);
                    }
                    x += 100;
                }
            }
        }

        private void fld_btnOK_Click(object sender, EventArgs e)
        {
            ErrorMessage = String.Empty;
            TreeList = GetAttributesAtLevel(1, 0);
            if (!String.IsNullOrEmpty(ErrorMessage))
            {
                MessageBox.Show(ErrorMessage, "#Message#", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private ProductAttributeList GetAttributesAtLevel(int level, int parentID)
        {
            ProductAttributeList result = new ProductAttributeList();
            if (level <= NumLevels)
            {
                ICInventoryStocksController objInventoryStocksController = new ICInventoryStocksController();
                int count = 0;
                foreach (Control ctrl in fld_pnlAttributes.Controls)
                {
                    if (ctrl.GetType() == typeof(DevExpress.XtraEditors.CheckEdit))
                    {
                        DevExpress.XtraEditors.CheckEdit chk = (DevExpress.XtraEditors.CheckEdit)ctrl;
                        if (chk.Properties.AccessibleName == level.ToString() && chk.Checked)
                        {
                            ICProductAttributesInfo objProductAttributesInfo = new ICProductAttributesInfo();
                            objProductAttributesInfo.FK_ICDepartmentAttributeID = ((ICDepartmentAttributeValuesInfo)ctrl.Tag).FK_ICDepartmentAttributeID;
                            objProductAttributesInfo.FK_ICDepartmentAttributeValueID = ((ICDepartmentAttributeValuesInfo)ctrl.Tag).ICDepartmentAttributeValueID;
                            objProductAttributesInfo.Selected = true;
                            objProductAttributesInfo.ICProductAttributeParentID = parentID;
                            //If attribute already exists, get back ID to avoid duplicating record when saving
                            ICProductAttributesInfo objOldProductAttributesInfo = (ICProductAttributesInfo)TreeList.GetObjectByTemplateObject(objProductAttributesInfo, "FK_ICDepartmentAttributeID", "FK_ICDepartmentAttributeValueID", "ICProductAttributeParentID");
                            if (objOldProductAttributesInfo != null)
                            {
                                objProductAttributesInfo.ICProductAttributeID = objOldProductAttributesInfo.ICProductAttributeID;
                                objProductAttributesInfo.FK_ICDepartmentID = objOldProductAttributesInfo.FK_ICDepartmentID;
                                objProductAttributesInfo.FK_ICProductGroupID = objOldProductAttributesInfo.FK_ICProductGroupID;
                                objProductAttributesInfo.ICProductAttributeSerialNo = objOldProductAttributesInfo.ICProductAttributeSerialNo;
                                objProductAttributesInfo.ICInventoryStockQuantity = objOldProductAttributesInfo.ICInventoryStockQuantity;
                                objProductAttributesInfo.ICInventoryStockSaleOrderQuantity = objOldProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                            }
                            objProductAttributesInfo.SubList = GetAttributesAtLevel(level + 1, objProductAttributesInfo.ICProductAttributeID);
                            result.Add(objProductAttributesInfo);
                            result.OriginalList.Add((THSTreeListObject)objProductAttributesInfo.Clone());
                            count++;
                        }
                    }
                    else if (ctrl.GetType() == typeof(DevExpress.XtraEditors.PanelControl))
                        GetAttributesAtLevel(level, parentID);
                }
                if (count == 0)
                    ErrorMessage = "You must select at least one attribute at each level";
            }
            
            return result;
        }

        private void fld_btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}